##
## This file is part of the django-oidauth project.
##
## Copyright (C) 2009 Filippo Santovito <http://mementomori.myvidoop.com/>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License version 3 as
## published by the Free Software Foundation;
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
##

from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext as _
from django.conf import settings

from openid.yadis import xri

class OpenidSigninForm(AuthenticationForm):
    """ signin form """                    

    def __init__(self, request=None, *args, **kwargs):
        super(OpenidSigninForm,self).__init__(request, *args, **kwargs)

        try:        
            if kwargs['data']['username'] <> '' and kwargs['data']['password'] <> '':
                self.fields['openid_url'].required = False                           

            if kwargs['data']['openid_url'] <> '':
                self.fields['username'].required = False
                self.fields['password'].required = False
        except:                                         
            pass                                        
                                                        
    openid_url = forms.CharField(label='openid_url', max_length=255)
                                                                    
    def clean_openid_url(self):                                     
        """ test if openid is accepted """                          
        openid_url = self.cleaned_data['openid_url']                
        if xri.identifierScheme(openid_url) == 'XRI' and getattr(settings, 'OPENID_DISALLOW_INAMES', False):
            raise forms.ValidationError(_('i-names are not supported'))                                     
        return self.cleaned_data['openid_url'] 
